package com.wuliu.system.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.ThreadLocalContext;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.BasTruckInfoMapper;
import com.wuliu.system.domain.BasTruckInfo;
import com.wuliu.system.service.IBasTruckInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 车辆资料Service业务层处理
 *
 * @author realue
 * @date 2023-10-24
 */
@Service
public class BasTruckInfoServiceImpl extends ServiceImpl<BasTruckInfoMapper, BasTruckInfo> implements IBasTruckInfoService {
    @Resource
    private BasTruckInfoMapper basTruckInfoMapper;

    /**
     * 查询车辆资料
     *
     * @param id 车辆资料主键
     * @return 车辆资料
     */
    @Override
    public BasTruckInfo selectBasTruckInfoById(String id) {
        return basTruckInfoMapper.selectBasTruckInfoById(id);
    }

    /**
     * 查询车辆资料列表
     *
     * @param basTruckInfo 车辆资料
     * @return 车辆资料
     */
    @Override
    public List<BasTruckInfo> selectBasTruckInfoList(BasTruckInfo basTruckInfo) {
        return basTruckInfoMapper.selectBasTruckInfoList(basTruckInfo);
    }

    /**
     * 新增车辆资料
     *
     * @param basTruckInfo 车辆资料
     * @return 结果
     */
    @Override
    public int insertBasTruckInfo(BasTruckInfo basTruckInfo) {
        long count = basTruckInfoMapper.selectCount(new LambdaQueryWrapper<BasTruckInfo>()
                .eq(BasTruckInfo::getSysAccount, basTruckInfo.getSysAccount())
                .eq(BasTruckInfo::getPlateNo, basTruckInfo.getPlateNo()));
        if (count > 0) {
            throw new ServiceException("车辆编号重复");
        }
        basTruckInfo.setId(IdUtil.simpleUUID());
        basTruckInfo.setCreateTime(DateUtils.getNowDate());
        return basTruckInfoMapper.insertBasTruckInfo(basTruckInfo);
    }

    /**
     * 修改车辆资料
     *
     * @param basTruckInfo 车辆资料
     * @return 结果
     */
    @Override
    public int updateBasTruckInfo(BasTruckInfo basTruckInfo) {
        basTruckInfo.setUpdateTime(DateUtils.getNowDate());
        return basTruckInfoMapper.updateBasTruckInfo(basTruckInfo);
    }

    /**
     * 批量删除车辆资料
     *
     * @param ids 需要删除的车辆资料主键
     * @return 结果
     */
    @Override
    public int deleteBasTruckInfoByIds(String[] ids) {
        return basTruckInfoMapper.deleteBasTruckInfoByIds(ids);
    }

    /**
     * 删除车辆资料信息
     *
     * @param id 车辆资料主键
     * @return 结果
     */
    @Override
    public int deleteBasTruckInfoById(String id) {
        return basTruckInfoMapper.deleteBasTruckInfoById(id);
    }

    /**
     * 查询车辆列表 app端
     *
     * @return
     */
    @Override
    public List<BasTruckInfo> getTrucks() {
        SysAppletUser user = SecurityUtils.getAppletUser();
        return basTruckInfoMapper.selectList(new LambdaQueryWrapper<BasTruckInfo>()
                .eq(BasTruckInfo::getSysAccount, user.getSysAccount()));
    }
}
